<?php

namespace App\Console\Commands;

use App\Model\Permissions;
use Illuminate\Console\Command;
use Spatie\Permission\Models\Permission;

/**
 * 创建cms后台菜单
 */
class CreateMenuCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'cmsmenu:create';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '创建cms后台菜单';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $this->createIllustratedHandbookMenu();
    }

    /**
     * 创建`图鉴管理`菜单
     */
    private function createIllustratedHandbookMenu()
    {
        //菜单在礼物管理之下
        $topMenuName = 'gift';
        //先查询顶级菜单是否存在
        $topMenu = Permissions::query()->where('pid', 0)->where('name', $topMenuName)->first();
        if (!$topMenu->count()) {//顶级菜单不存在退出
            $this->error('菜单`礼物管理`不存在');
            die();
        }

        $id = $topMenu->id;//顶级菜单id
        //图鉴列表菜单
        $menuName = 'illustratedhandbook_index';
        $menu = Permissions::query()->where('name', $menuName)->first();
        if (!$menu) {//菜单不存在，添加
            Permission::create(['name' => $menuName, 'desc' => '图鉴列表', 'pid' => $id]);
        }

        $this->info('添加成功');
    }
}
